﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Skill : MonoBehaviour
{
    [SerializeField] private float moveSpeed = 10f;
    [SerializeField] private float attackDamage = 2f;
    [SerializeField] private float minDistanceToDealDamage = 0.1f;

    private Monster _monsterTarget;
    private bool _bomb = false;
    public bool IfBomb => _bomb; 

    private void Update()
    {
        if (_monsterTarget != null)
        {
            MoveSkill();
            RoatateSkill();
        }
    }

    private void MoveSkill()
    {
        transform.position = Vector2.MoveTowards(transform.position, _monsterTarget.transform.position, moveSpeed * Time.deltaTime);
        float distanceToTarget = (_monsterTarget.transform.position - transform.position).magnitude;
        if (distanceToTarget < minDistanceToDealDamage)
        {
            _monsterTarget.DealDamage(attackDamage);
            ObjectPooler.ReturnToPool(gameObject);
        } 
    }

    private void RoatateSkill()
    {
        Vector3 monsterPos = _monsterTarget.transform.position - transform.position;
        float angle = Vector3.SignedAngle(transform.up, monsterPos, transform.forward);
        transform.Rotate(0f, 0f, angle+90);
    }

    public void SetMonster(Monster monster)
    {
        _monsterTarget = monster;
    }
}
